/**
 * 递归无级树
 * @param {Array} data  
 * @param {Object} parent 顶层父级
 * @param {String} pk 主键名
 * @param {String} ck 子键名
 * @param {String} child 子项名
 */
export function hTree(data, parent, pk, ck, child) {

	if (data.length == 0) {
		return;
	}
	pk = pk || "key",
		ck = ck || "key_self",
		child = child || "children";
	if (!parent[child]) {
		parent[child] = [];
	}
	//root parent
	var root = data.filter(t => t[ck] === 0);
	//console.log("第一层",root);
	if (root.length > 0) {
		for (var i = 0; i < root.length; i++) {		 
			var ix = data.findIndex(t => t[pk] == root[i][pk]);
			data.splice(ix, 1);
		}
		for (var i = 0; i < root.length; i++) {
			var ritem = root[i];
			ritem[child] = [];
			hTree(data, ritem, pk, ck, child);
			parent[child].push(ritem);
			//i--;
		}
		return;
	}

	//children
	var children = data.filter(t => t[ck] == parent[pk]);
	if (children.length > 0) {

		for (var i = 0; i < children.length; i++) {
			var ritem = children[i];
			var ix = data.findIndex(t => t[pk] == ritem[pk]);
			data.splice(ix, 1);
		}
		for (var i = 0; i < children.length; i++) {
			var ritem = children[i];
			ritem[child] = [];
			hTree(data, ritem, pk, ck, child);
			parent[child].push(ritem);
			//i--;
		};

	}

}